package com.sds.sweng.swat.miplatform.service.figures.components;

import org.apache.commons.lang.StringUtils;
import org.dom4j.Element;
import org.dom4j.Node;
import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.draw2d.PositionConstants;

import com.sds.sweng.swat.miplatform.service.graphics.ComponentDrawer;
import com.sds.sweng.swat.miplatform.service.graphics.ComponentUtils;

public class SimpleComponent extends Label implements MiplatformComponent {

	protected Node component;
	protected Node parent;
	protected ComponentDrawer drawer;

	public SimpleComponent(Node component, Node parent) {
		this.component = component;
		this.parent = parent;

		setBounds(ComponentUtils.getComponentBound(component, parent));
		setSize(getBounds().width, getBounds().height);
		setBorder(new LineBorder(ColorConstants.black));

		initComponent();
	}

	protected void initComponent() {
		setText(component.valueOf("@Text"));
		setLabelAlignment(PositionConstants.LEFT);
	}

	public void hasBorder(boolean border) {
		if (!border) {
			setBorder(null);
		}
	}

	public void setDrawer(ComponentDrawer drawer) {
		this.drawer = drawer;
	}

	public Node getNode() {
		return component;
	}

	public String getId() {
		Element parent = component.getParent();
		String id = component.valueOf("@Id");

		while (!"Form".equals(parent.getName())) {
			String parentId = parent.valueOf("@Id");

			if (!StringUtils.isEmpty(parentId)) {
				id = parentId + "." + id;
			}

			parent = parent.getParent();
		}

		return id;
	}

}
